package cn.iocoder.yudao.module.dine.desk.service;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.business.basic.service.CrudService;
import cn.iocoder.yudao.framework.business.basic.service.impl.BaseService;
import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.framework.qrcode.pojo.QrCodeDineInType;
import cn.iocoder.yudao.framework.qrcode.util.QrCodeUtils;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.dine.desk.convert.DeskConvert;
import cn.iocoder.yudao.module.dine.desk.dao.DeskMapper;
import cn.iocoder.yudao.module.dine.desk.pojo.dto.DeskQueryDTO;
import cn.iocoder.yudao.module.dine.desk.pojo.dto.DeskSaveDTO;
import cn.iocoder.yudao.module.dine.desk.pojo.po.Desk;
import cn.iocoder.yudao.module.dine.desk.pojo.vo.DeskVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserLastShopId;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 服务接口类
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/04/11
 */
@Service
public class DeskService extends BaseService<Desk, DeskVO, DeskQueryDTO> implements CrudService<Desk, DeskVO, DeskSaveDTO, DeskQueryDTO> {
    @Resource
    private DeskMapper deskMapper;

    @Resource
    @Lazy // 延迟加载，避免循环依赖
    private ShopInfoApi shopInfoApi;
    @Resource
    @Lazy // 延迟加载，避免循环依赖
    private MemberUserApi memberUserApi;

    @Override
    public DeskMapper getInjectedMapper() {
        return deskMapper;
    }

    @Override
    public Desk convertToEntity(DeskSaveDTO param) {
        return DeskConvert.INSTANCE.toEntity(param);
    }

    @Override
    public void beforeCreate(Desk entity, @Nullable Long userId) {
        entity.setShopId(getLoginUserLastShopId());
        entity.setStatus(0);
        entity.setLastOrderStatus(-1);
    }

    @Override
    public void beforeUpdate(Desk entity) {
        Assertions.owner(entity.getShopId(), getLoginUserLastShopId());
    }

    @Override
    public void beforeDelete(Desk entity) {
        Assertions.owner(entity.getShopId(), getLoginUserLastShopId());
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<Desk> buildQueryWrapper(DeskQueryDTO param) {
        CoreQueryWrapper<Desk> wrapper = super.buildQueryWrapper(param);
        wrapper.eqIfPresent("category_id", param.getCategoryId());
        wrapper.eqIfPresent("status", param.getStatus());
        wrapper.likeIfPresent("no", param.getNo());
        return wrapper;
    }

    @Override
    public DeskVO convertToViewObject(Desk entity) {
        return DeskConvert.INSTANCE.toViewObject(entity);
    }

    @Override
    public List<DeskVO> convertToViewObject(List<Desk> entities) {
        Set<Long> shopIds = convertSet(entities, Desk::getShopId);
        Map<Long, ShopInfoDTO> shops = shopInfoApi.getShopInfoMap(shopIds);
        Set<Long> userIds = convertSet(shops.values(), ShopInfoDTO::getUserId);
        Map<Long, UserSimpleVO> users = memberUserApi.getUserSimpleMap(userIds);
        List<DeskVO> list = super.convertToViewObject(entities);
        for (DeskVO vo : list) {
            ShopInfoDTO shop = shops.getOrDefault(vo.getShopId(), ShopInfoDTO.FALLBACK);
            vo.setShop(shop);
            QrCodeDineInType qrCodeDineInType = new QrCodeDineInType();
            qrCodeDineInType.setShopId(vo.getShopId());
            qrCodeDineInType.setDeskNo(vo.getNo());
            qrCodeDineInType.setMemberNo(users.getOrDefault(shop.getUserId(), UserSimpleVO.ANONYMOUS).getNo());
            vo.setQrcode(QrCodeUtils.generatePngAsBase64Uri(qrCodeDineInType, shop.getLogo()));
        }
        return list;
    }

}
